package org.wadael.waverobotfrmwrk.meta;

import com.google.wave.api.Blip;
import com.google.wave.api.Wavelet;

/**
 * A class that you CAN use as motherclass of your robot, so that it's only a
 * matter of subclassing it and implementing one method for the CODE part of
 * your wave robot (some configuration needed).
 * 
 * Advanced users may use it but for quick development, using classes from the
 * simpleRobots package is advised.
 * 
 * @author jerome baton : wadael (http://www.wadael.org , http://twitter.com/wadael)
 * 
 * This code is published under the licence indicated 
 * on the website  http://code.google.com/p/wadrobotframework (or replacement site).
 */
public abstract class RobotTemplateServlet extends
		com.google.wave.api.AbstractRobotServlet {

	private static final long serialVersionUID = 1980415784799752877L;

	/**
	 * A standard wave method here overridden so to ease your robot creation. Do
	 * not override unless you know what you're doing and do not want to use
	 * this framework. This method's code is based on the code provided in the
	 * wave robot tutorial published by Google.
	 */
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		if (bundle.wasSelfAdded()) {
			Blip blip = wavelet.appendBlip();
			reactToRobotAddedToAWavelet(bundle, blip);
			return;
		} else {
			for (Event e : bundle.getEvents()) {
				if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {
					reactToParticipantsChangement(bundle, wavelet, e);
				}

				if ((e.getType() == EventType.BLIP_SUBMITTED)) {
					if (getDisplayName() != null) {
						if (e.getModifiedBy().indexOf( getDisplayName() ) != -1) {
							reactToOwnSubmittedBlip(bundle, e);
							return;
						}
					}
					Blip blip = e.getBlip();
					reactToSubmittedBlip(bundle, blip, e);
				}
			}
		}
	}

	/**
	 * Defines what your robot should do when a blip has been added. MANDATORY
	 * for simple robots.
	 * 
	 * @param bundle
	 * @param e
	 */
	protected abstract void reactToSubmittedBlip(RobotMessageBundle bundle,
			Blip blip, Event event);

	/**
	 * Override-me
	 * 
	 * Defines what to do when your robot has added a blip. HINT : beware of
	 * infinite loops !!
	 * 
	 * Not mandatory for simple robots.
	 * 
	 * @param bundle
	 * @param e
	 */
	protected void reactToOwnSubmittedBlip(RobotMessageBundle bundle,
			Event event) {
		// TODO
	}

	/**
	 * Override-me
	 * 
	 * Defines what to do when the participants have changed (more or less). Not
	 * mandatory for simple robots.
	 */
	protected void reactToParticipantsChangement(RobotMessageBundle bundle,
			Wavelet wavelet, Event e) {
	}

	/**
	 * Override-me
	 * 
	 * Defines what to do when THIS (YOUR) robot is added to a wavelet. Usually,
	 * it should present itself.
	 * 
	 * @param bundle
	 * @param blip
	 */
	protected void reactToRobotAddedToAWavelet(RobotMessageBundle bundle,
			Blip blip) {
		String tmp = getRobotSelfIntroduction();
		if (tmp != null) {
			TextView textView = blip.getDocument();
			textView.append(tmp);
		}
	}

	/**
	 * Returns a text that is blipped when the robot is added to a wavelet. Use
	 * it to introduce the robot's behaviour.
	 * 
	 * Default is to display 'hello' in French, English, Hindi, Arab and
	 * Romanian
	 * 
	 * @return
	 */
	protected String getRobotSelfIntroduction() {
		return "Bonjour! Hello ! Namaste ! Salam ! buna ziua !";
	}

	/**
	 * Returns the display name you wish to give your robot.
	 * This is displayed in GW when help is asked for (advanced robots only).
	 *  
	 * Overload this
	 * @return
	 */
	protected String getDisplayName() {
		return "Robot Made w/ WadRobotFramework";
	}

}
